package io.mft.runtime.function.process.executor;

import java.util.Map;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.mft.api.domain.Api;
import io.mft.api.domain.DataAccessRegulation;
import io.mft.api.domain.InputParameter;
import io.mft.api.domain.InputParameter.InputParameterDataType;
import io.mft.commons.function.ExecuteReturn;
import io.mft.commons.function.RequestValue;
import io.mft.commons.function.response.kv.KVMap;
import io.mft.commons.function.response.kv.KVMapList;
import io.mft.runtime.function.ApiContext;
import io.mft.runtime.function.ApiFunctionRequest;
import io.mft.runtime.function.process.ApiFunctionProcessExecutor;
/**
 * 输入数据访问操作处理执行器
 * @说明：TODO
 * @类型名称：DataAccessProcessExecutor
 * @创建者: 胡常建  
 * @创建时间: 2021年1月26日 下午4:41:00
 * @修改者: 胡常建  
 * @修改时间: 2021年1月26日 下午4:41:00
 */
public class DataAccessProcessExecutor extends ApiFunctionProcessExecutor<DataAccessRegulation>{
	Logger logger=LoggerFactory.getLogger(getClass());
	
	public String buildInputDataAccessProcessExecutorId(DataAccessRegulation dataAccessRegulation) {
		String beanId=dataAccessRegulation.getType().DatasourceType().name()+"."+dataAccessRegulation.getType().name()+"."+dataAccessRegulation.getDataType().name()+".DataAccessProcessExecutor";
		return beanId;
	}
	
	private RequestValue fixRequestValue(Api api,RequestValue data) {
		Map<String, InputParameter> map=api.getInput().inputParameterMap();
		Set<String> keys=data.keySet();
		RequestValue requestValue=new RequestValue();
		keys.forEach(key->{
			 boolean exist=map.containsKey(key);
			 if (exist) {
				 InputParameter inputParameter=map.get(key);
				 Object v=data.get(key);
				 if (inputParameter.getDataType()==InputParameterDataType.json||inputParameter.getDataType()==InputParameterDataType.jsonArray) {
					KVMapList list=data.getBean(key);
					KVMapList actualList=new KVMapList();
					list.forEach(kv->{
						KVMap actualV=new KVMap();
						Set<String> kvkeys=kv.keySet();
						kvkeys.forEach(k->{
							actualV.set(api.getInput().actualKey(k), kv.get(k));
						});
						actualList.add(actualV);
					});
					requestValue.set(map.get(key).getActualKey(), actualList);
				 }else {
					requestValue.set(map.get(key).getActualKey(), v);
				}
			}
		});
		//logger.info("功能号:{}->>>DataAccessProcessExecutor->>>fixRequestValue参数:{}",api.getKey(),requestValue);
		return requestValue;
	}
	@Override
	public ExecuteReturn doExecute(ApiContext context) {
		if (context.isSuccessReadCache()) {
			return ExecuteReturn.builder().build();
		}
		logger.debug("功能号:{}->>>逻辑单元-DataAccessProcessExecutor执行->>>原始参数:{}",context.getApiExecutorObject().getApi().getKey(),context.getRequest().getRequestValue());
		RequestValue requestValue=fixRequestValue(context.getApiExecutorObject().getApi(), context.getRequest().getRequestValue());
		ApiFunctionRequest request=new ApiFunctionRequest(requestValue);
		logger.debug("功能号:{}->>>逻辑单元-DataAccessProcessExecutor执行->>>修正参数:{}",context.getApiExecutorObject().getApi().getKey(),requestValue);
		ApiContext apiContext=new ApiContext(context.getApiExecutorObject(), request, context.getResponse());
		DataAccessRegulation dataAccessRegulation=getRegulation();
		String pluginId=dataAccessRegulation.getType().DatasourceType().name();
		return executePluginFunction(apiContext, dataAccessRegulation, pluginId, buildInputDataAccessProcessExecutorId(dataAccessRegulation));
	}


}
